package com.panda.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.panda.enums.AppHttpCodeEnum;

import java.io.Serializable;

// 返回制定格式的json数据  ： 如果属性返回值为空，则不返回任何内容
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RespondResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

//    构造方法：初始化一个操作成功的应答对象（属性值后续根绝可进行对应修改），目的定义一个无参构造
    public RespondResult() {
        this.code = AppHttpCodeEnum.SUCCESS.getCode();
        this.msg = AppHttpCodeEnum.SUCCESS.getMsg();
    }

//    根据不同的操作状态码，生成不同的应答对象
    public RespondResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    //    根据不同的应答信息，生成不同的应答对象
    public RespondResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    //    根据操作状态码和不同的应答信息，生成不同的应答对象
    public RespondResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

//  构造一个请求发生错误的应答对象，改变原有无参构造对象的对应属性(返回具体操作状态码和操作结果信息)
    public static <T> RespondResult<T> errorResult(int code, String msg) {
        RespondResult<T> result = new RespondResult<>();
        return result.error(code, msg);
    }
//    构造一个请求操作成功的应答对象，默认使用原有定义的成功属性（默认使用成功的状态码和操作信息）
    public static <T> RespondResult<T> okResult() {
        return new RespondResult<>();
    }
//    构造一个根据具体状态码和应答信息的应答对象(返回具体操作状态码和操作结果信息)
    public static <T> RespondResult<T> okResult(int code, String msg) {
        RespondResult<T> result = new RespondResult<>();
        return result.ok(code, null, msg);
    }
//    构造一个根据具体数据的应答对象（返回具体数据）
    public static  <T> RespondResult<T> okResult(T data) {
        RespondResult<T>  result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMsg());
        if(data!=null) {
            result.setData(data);
        }
        return result;
    }

//    封装一个响应对象
    public static <T> RespondResult<T> errorResult(AppHttpCodeEnum enums){
        return setAppHttpCodeEnum(enums,enums.getMsg());
    }
    //    封装一个具有具体响应信息的响应对象
    public static <T> RespondResult<T> errorResult(AppHttpCodeEnum enums, String msg){
        return setAppHttpCodeEnum(enums,msg);
    }

//    构造枚举对象对应状态和对应信息的应答对象
    public static <T> RespondResult<T> setAppHttpCodeEnum(AppHttpCodeEnum enums){
        return okResult(enums.getCode(),enums.getMsg());
    }

//    构造具体信息的应答对象
    private static <T> RespondResult<T> setAppHttpCodeEnum(AppHttpCodeEnum enums, String msg){
        return okResult(enums.getCode(),msg);
    }

    public RespondResult<T> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public RespondResult<T> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public RespondResult<T> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public RespondResult<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}